﻿using itemApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Security;

namespace itemApi.Controllers
{
    public class LoginController : ApiController
    {
        [HttpPost]
        public IHttpActionResult Login([FromBody] Users us) {
            using (crmEntities DB = new crmEntities())
            {
                var thedata = DB.Users.SingleOrDefault(x => x.loginName == us.loginName);

                if (thedata != null)
                {
                    if (us.loginName != thedata.loginName)
                    {
                        return Json(new { code = -1, msg = "用户名错误" });
                    }
                    else
                    {
                        if (us.password != thedata.password)
                        {
                            return Json(new { code = -1, msg = "密码错误" });
                        }
                        else
                        {
                            if (thedata.isLockout == 1)
                            {
                                return Json(new { code=-1, msg = "账号已经锁定" });
                            }
                            else
                            {
                                int mytoken = thedata.uid;
                                return Json(new
                                {
                                    code = 0,
                                    msg = "登录成功",
                                    token=mytoken                           
                                });
                            }
                        }
                    }
                }
                else
                {
                    return Json(new { code = -1, msg = "用户名错误" });
                    return Json(new { code = -1,msg = "用户名错误" });
                }
            }
        }
    }
}
